package com.person.jc.service.base.sign;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdSignService extends Service {
	
	public final static BdSignService me = new BdSignService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select t.*,t1.user_name from bd_sign t, ts_user t1 where t.user_id = t1.user_id " +
				" and t1.status <> 3");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("userName")))){
			sql.append(" and t1.user_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("signId")))){
			sql.append(" and t.sign_id = '"+ temp +"'");
		}
		sql.append(" order by t1.user_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_sign (user_id, img_url, remark) values (?, ?, ?)";
		Db.update(sql, map.get("userId"), map.get("imgUrl"), map.get("remark"));
		super.setCRUD("个人签名管理", "C", "新增签名信息："+map.get("userId"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_sign set user_id = ? , img_url = ?, remark = ? where sign_id = ?";
		Db.update(sql, map.get("userId"), map.get("imgUrl"), map.get("remark"), map.get("signId"));
		super.setCRUD("个人签名管理", "U", "修改签名信息，编号："+map.get("signId"));
	}
	
	public void delete(String signId){
		String sql = " delete from bd_sign where sign_id = ? ";
		Db.update(sql, signId);
		super.setCRUD("个人签名管理", "D", "删除签名信息，编号："+signId);
	}
	
	public long checkName(String userId, String signId){
		String sql = "select count(1) from bd_sign where user_id = ? ";
		if(Utils.isNotEmpty(signId)){
			sql += " and sign_id <> " + signId;
		}
		return Db.queryLong(sql, userId);
	}
}
